package com.quanjiaolian.tools;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 工具类：用于对用户密码的编解码及判定用户手机号是否为纯数字
 * @author Herson_Li
 *
 */
public class Utils {
	
	 static BASE64Encoder encoder = new sun.misc.BASE64Encoder();    
	 static BASE64Decoder decoder = new sun.misc.BASE64Decoder();  
	
	/**
	 * 解决中文乱码问题
	 * @param param
	 * @return
	 */
	public static String convertString(String param) {
		String str = "";
		try {
			str = new String(param.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	/**  
     * 编码  
     * @param bstr  
     * @return String  
     */   
    public static String encode(byte[] bstr){   
    	return new sun.misc.BASE64Encoder().encode(bstr);   
    }   
   
    /**  
     * 解码  
     * @param str  
     * @return string  
     */   
    public static byte[] decode(String str){   
	    byte[] bt = null;   
	    try {   
	        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();   
	        bt = decoder.decodeBuffer(str);
	    } catch (IOException e) {   
	        e.printStackTrace();   
	    }   
	   
	        return bt;   
    }   
    
    /**
     * 判断是否为纯数字
     * @param str
     * @return
     */
    public static boolean isNumberic(String str)
    {
    	for(int i=0;i<str.length();i++)
    	{
    		if(!Character.isDigit(str.charAt(i)))
    		{
    			return false;
    		}
    	}
    	return true;
    }
    /**
     * 将图片转成二进制字符串
     * @return
     */
    static String getImageBinary(){    
        File f = new File("f://1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg");           
        BufferedImage bi;    
        try {    
            bi = ImageIO.read(f);    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();    
            ImageIO.write(bi, "jpg", baos);    
            byte[] bytes = baos.toByteArray();    
                
            return encoder.encodeBuffer(bytes).trim();    
        } catch (IOException e) {    
            e.printStackTrace();    
        }    
        return null;    
    }    
    /**
     * 将二进制字符串转为图片    
     * @param base64String
     */
    static void base64StringToImage(String base64String){    
        try {    
            byte[] bytes1 = decoder.decodeBuffer(base64String);    
                
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);    
            BufferedImage bi1 =ImageIO.read(bais);    
            File w2 = new File("c://QQ.bmp");//可以是jpg,png,gif格式    
            ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片，此处不需改动    
        } catch (IOException e) {    
            e.printStackTrace();    
        }    
    }    
    /**
     * 生成短信验证码
     * @return
     */
    public static String makeCode(){
    	String code = System.currentTimeMillis()%100000+"";
		for(int i = code.length();i<5;i++){
			code+=i;
		}
    	return code;
    }
    
    public static void main(String[] args) {
//		System.out.println(isNumberic("dGVzdDEyMzQ1Njc4"));
//		System.out.println(new String(decode("emhhbmd4aWFvc2FuMTIz")));
    	System.out.println(makeCode());
	}
}
